﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using Meqs.Entity;
using Meqs.Entity.Views;
using Meqs.Logic.Interface;
using Meqs.Utilities;

public partial class Credit_Teacher_Reprots_StudentFile : ContainerBasePage
{
    protected static int StuID
    {
        get
        {
            int id;
            if (int.TryParse(HttpContext.Current.Request["id"], out id))
                return id;
            return -1;
        }
    }

    protected static String StuCode
    {
        get
        {
            String code;
            code = HttpContext.Current.Request["code"];
            return code;
        }
    }

    protected static String StuName
    {
        get
        {
            String name;
            name = HttpUtility.UrlDecode(HttpContext.Current.Request["name"]);
            return name;
        }
    }

    private CauseFormula causeFormula;
    private CauseLevelTool causeLevelTool;

    protected IList<CreditHistory> creditList = null;
    protected IList<StudentHistory> historyList = null;
    private IList<StudentMoralityScoreExView> moralityList = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
            btnBind_Click(btnBind, new EventArgs());
    }

    protected void btnBind_Click(object sender, EventArgs e)
    {
        IList<ClsInfo> l = new List<ClsInfo>();
        l.Add(new ClsInfo());
        Repeater1.DataSource = l;
        Repeater1.DataBind();
        WriteContext();
    }

    protected string WriteContext()
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<h2>罗芳中学学生成长档案记录表</h2><br/><table  class=\"useList1\">");
        #region 构造学生信息部分
        StudentInfo si = Container<IStudentInfoLogic>.Instance.FindById(StuID);
        sb.Append(
            String.Format("<tr><td>姓名</td><td>{0}</td><td>性别</td><td>{1}</td><td>出生年月</td><td>{2}</td></tr>",
                si.StudentName, si.Sexy == 0 ? "男" : "女", si.Birthday.ToString("yyyy-MM"))
            );
        sb.Append(String.Format("<tr><td>学号</td><td colspan=5>{0}</td></tr>", si.StudentCode));
        sb.Append(String.Format("<tr><td>入学时间</td><td colspan=5>{0}</td></tr>", si.InSchoolDate.ToString("yyyy-MM")));
        #endregion
        #region 构造德育成长档案部分
        sb.Append("<tr><td colspan=6>德育成长档案</td></tr>");
        causeFormula = new CauseFormula(SchoolMark, 0);
        moralityList = Container<IMoralityPersonCatchLogic>.Instance.GetMoralityHistoryReportEx(SchoolMark, StuID);
        causeLevelTool = new CauseLevelTool(SchoolCommon.Sch_SchoolInfo.SchoolYear, SchoolCommon.Sch_SchoolInfo.SchoolTerm, SchoolMark);
        Dictionary<int, StudentMoralityScoreExView> moralityScore = new Dictionary<int, StudentMoralityScoreExView>();
        foreach (StudentMoralityScoreExView view in moralityList)
        {
            int grade = 0;
            if (view.Term == 2)
                grade = view.Grade + 100;
            else
                grade = view.Grade;

            if (!moralityScore.ContainsKey(grade))
                moralityScore.Add(grade, view);
        }

        IList<MoralityTypeCatchView> mptcList1 = new List<MoralityTypeCatchView>();
        IList<MoralityTypeCatchView> mptcList2 = new List<MoralityTypeCatchView>();
        // 查询类别
        if(moralityScore.ContainsKey(7))
            mptcList1 = Container<IMoralityPersonTypeCatchLogic>.Instance.GetStuTypeScore(SchoolMark, StuID, moralityScore[7].SchoolYear, moralityScore[7].Term, "Student_Month_Action");
        if (moralityScore.ContainsKey(107))
            mptcList2 = Container<IMoralityPersonTypeCatchLogic>.Instance.GetStuTypeScore(SchoolMark, StuID, moralityScore[107].SchoolYear, moralityScore[107].Term, "Student_Month_Action");
        sb.Append(String.Format("<tr><td rowspan={2}>七年级</td><td rowspan={3}>{0}学年上学期</td><td>本学期德育评定等级</td><td colspan=3>{1}</td></tr>",
            (moralityScore.ContainsKey(7) ? moralityScore[7].SchoolYear.ToString() : "-"),
            causeLevelTool.GetCauseLevel(moralityScore.ContainsKey(7) ? moralityScore[7].SchoolYear : 0, 1,
            causeFormula.GetMoralScore(moralityScore.ContainsKey(7) ? moralityScore[7].Score : 0)), 
            mptcList1.Count + mptcList2.Count + 4, mptcList1.Count + 2));
        foreach (MoralityTypeCatchView view in mptcList1)
        {
            sb.Append(String.Format("<tr><td>{0}</td><td colspan=3>{1}</td></tr>",
                view.Caption, view.Score.HasValue?view.Score:0));
        }
        sb.Append(String.Format("<tr><td>教师评语</td><td colspan=3>{0}</td></tr>",
            moralityScore.ContainsKey(7) ? moralityScore[7].EvalText : "-"));

        // 查询类别
        sb.Append(String.Format("<tr><td rowspan={2}>{0}学年下学期</td><td>本学期德育评定等级</td><td colspan=3>{1}</td></tr>",
            moralityScore.ContainsKey(107) ?  moralityScore[107].SchoolYear.ToString() : "-",
            causeLevelTool.GetCauseLevel(moralityScore.ContainsKey(107) ? moralityScore[107].SchoolYear : 0, 2,
            causeFormula.GetMoralScore(moralityScore.ContainsKey(107) ? moralityScore[107].Score : 0)), mptcList2.Count + 2));
        foreach (MoralityTypeCatchView view in mptcList2)
        {
            sb.Append(String.Format("<tr><td>{0}</td><td colspan=3>{1}</td></tr>",
                view.Caption, view.Score.HasValue ? view.Score : 0));
        }
        sb.Append(String.Format("<tr><td>教师评语</td><td colspan=3>{0}</td></tr>",
            moralityScore.ContainsKey(107) ? moralityScore[107].EvalText : "-"));
       ///////////////////////////////////////////////////////
        // 查询类别
        if (moralityScore.ContainsKey(8))
            mptcList1 = Container<IMoralityPersonTypeCatchLogic>.Instance.GetStuTypeScore(SchoolMark, StuID, moralityScore[8].SchoolYear, moralityScore[8].Term, "Student_Month_Action");
        if (moralityScore.ContainsKey(108))
            mptcList2 = Container<IMoralityPersonTypeCatchLogic>.Instance.GetStuTypeScore(SchoolMark, StuID, moralityScore[108].SchoolYear, moralityScore[108].Term, "Student_Month_Action");
        sb.Append(String.Format("<tr><td rowspan={2}>八年级</td><td rowspan={3}>{0}学年上学期</td><td>本学期德育评定等级</td><td colspan=3>{1}</td></tr>",
            (moralityScore.ContainsKey(8) ? moralityScore[8].SchoolYear.ToString() : "-"),
            causeLevelTool.GetCauseLevel(moralityScore.ContainsKey(8) ? moralityScore[8].SchoolYear : 0, 1,
            causeFormula.GetMoralScore(moralityScore.ContainsKey(8) ? moralityScore[8].Score : 0)), mptcList1.Count + mptcList2.Count + 4, mptcList1.Count + 2));
        foreach (MoralityTypeCatchView view in mptcList1)
        {
            sb.Append(String.Format("<tr><td>{0}</td><td colspan=3>{1}</td></tr>",
                view.Caption, view.Score.HasValue ? view.Score : 0));
        }
        sb.Append(String.Format("<tr><td>教师评语</td><td colspan=3>{0}</td></tr>",
            moralityScore.ContainsKey(8) ? moralityScore[8].EvalText : "-"));

        // 查询类别
        sb.Append(String.Format("<tr><td rowspan={2}>{0}学年下学期</td><td>本学期德育评定等级</td><td colspan=3>{1}</td></tr>",
            moralityScore.ContainsKey(108) ? moralityScore[108].SchoolYear.ToString() : "-",
            causeLevelTool.GetCauseLevel(moralityScore.ContainsKey(108) ? moralityScore[108].SchoolYear : 0, 2,
            causeFormula.GetMoralScore(moralityScore.ContainsKey(108) ? moralityScore[108].Score : 0)), mptcList2.Count + 2));
        foreach (MoralityTypeCatchView view in mptcList2)
        {
            sb.Append(String.Format("<tr><td>{0}</td><td colspan=3>{1}</td></tr>",
                view.Caption, view.Score.HasValue ? view.Score : 0));
        }
        sb.Append(String.Format("<tr><td>教师评语</td><td colspan=3>{0}</td></tr>",
            moralityScore.ContainsKey(108) ? moralityScore[108].EvalText : "-"));
        //////////////////////////////////////////////////////////////
        // 查询类别
        if (moralityScore.ContainsKey(9))
            mptcList1 = Container<IMoralityPersonTypeCatchLogic>.Instance.GetStuTypeScore(SchoolMark, StuID, moralityScore[9].SchoolYear, moralityScore[9].Term, "Student_Month_Action");
        if (moralityScore.ContainsKey(109))
            mptcList2 = Container<IMoralityPersonTypeCatchLogic>.Instance.GetStuTypeScore(SchoolMark, StuID, moralityScore[109].SchoolYear, moralityScore[109].Term, "Student_Month_Action");
        sb.Append(String.Format("<tr><td rowspan={2}>九年级</td><td rowspan={3}>{0}学年上学期</td><td>本学期德育评定等级</td><td colspan=3>{1}</td></tr>",
            (moralityScore.ContainsKey(9) ? moralityScore[9].SchoolYear.ToString() : "-"),
            causeLevelTool.GetCauseLevel(moralityScore.ContainsKey(9) ? moralityScore[9].SchoolYear : 0, 1,
            causeFormula.GetMoralScore(moralityScore.ContainsKey(9) ? moralityScore[9].Score : 0)), mptcList1.Count + mptcList2.Count + 4, mptcList1.Count + 2));
        foreach (MoralityTypeCatchView view in mptcList1)
        {
            sb.Append(String.Format("<tr><td>{0}</td><td colspan=3>{1}</td></tr>",
                view.Caption, view.Score.HasValue ? view.Score : 0));
        }
        sb.Append(String.Format("<tr><td>教师评语</td><td colspan=3>{0}</td></tr>",
            moralityScore.ContainsKey(9) ? moralityScore[9].EvalText : "-"));
        // 查询类别
        sb.Append(String.Format("<tr><td rowspan={2}>{0}学年下学期</td><td>本学期德育评定等级</td><td colspan=3>{1}</td></tr>",
            moralityScore.ContainsKey(109) ? moralityScore[109].SchoolYear.ToString() : "-",
            causeLevelTool.GetCauseLevel(moralityScore.ContainsKey(109) ? moralityScore[109].SchoolYear : 0, 2,
            causeFormula.GetMoralScore(moralityScore.ContainsKey(109) ? moralityScore[109].Score : 0)), mptcList2.Count + 2));
        foreach (MoralityTypeCatchView view in mptcList2)
        {
            sb.Append(String.Format("<tr><td>{0}</td><td colspan=3>{1}</td></tr>",
                view.Caption, view.Score.HasValue ? view.Score : 0));
        }
        sb.Append(String.Format("<tr><td>教师评语</td><td colspan=3>{0}</td></tr>",
            moralityScore.ContainsKey(109) ? moralityScore[109].EvalText : "-"));

        #endregion
        sb.Append("</table>");
        return sb.ToString();
    }

    [WebMethod]
    public static string GetExcel(ExcelTableCell[][] cells, int causeID)
    {
        string temp_path = HttpContext.Current.Server.MapPath("../../../");
        string save_path = temp_path + "download\\" + StuCode + "_profile.xls";
        HttpContext.Current.Session["filepath"] = save_path;
        ExcelUtil.ExcelExport(save_path, StuCode + "学生成长档案", 2, 0, cells);
        return "";
    }
}